<template>
  <div>
    子1
    <button @click="chval">changge</button>
    <div v-for="(v,k) in arr" :key="k" style="width: 300px;">
      {{v}}
      <cellInput v-model="v.val" :isRequire='true' @getForm="getForm" />
    </div>

  </div>
</template>

<script>
  import eventHub from './eventHub.js'
  import cellInput from './cellInput.vue'
  export default {
    components: {
      cellInput
    },
    data() {
      return {
        arr: [{ val: 2 },{ val: '' }],
        forms: []

      };
    },
    computed: {

    },
    created() {
      eventHub.$on('submit', this.lessionSubmit)

    },
    beforeDestroy() {
      eventHub.$off('submit', this.lessionSubmit)
    },
    mounted() {

    },
    methods: {

      getForm(formCom) {
          this.forms.push(formCom)
          console.log(this.forms)
      },
      checkForm(){
        let allPass = true
        let promises = this.forms.map(
          f => {
           return new Promise((rs, rj) => {
              f.validate(valid => {
                if (valid) {
                  rs(valid)
                } else {
                  allPass = false
                  console.log('xxx')
                  rj(valid)
                }
              })
            })

          })
            console.log(promises)
        // return promises
       return Promise.all(promises).then(valids => {
          if (allPass) {
            // 表单全部验证通过 则进行整理数据
             console.log('ok')
             alert(1)
            return [{a:1}]

          } else {
            console.log('error')
            return null
          }
        })
      },

      chval() {
        this.arr[0].val = 233
      },
      lessionSubmit(a) {
        // alert('子接受到父的==' + a)
        // eventHub.$emit('forms', 'kkkkkkkkkkk')
        eventHub.$emit('forms', this.checkForm())

      },
    },

    watch: {

    }
  };
</script>

<style scoped lang="less">

</style>
